package com.lanou.musicproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.SeekSingerActivity;
import com.lanou.musicproject.adapter.SingerListViewAdapter;
import com.lanou.musicproject.adapter.SongListViewAdapter;
import com.lanou.musicproject.bean.Singer;
import com.lanou.musicproject.bean.Song;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 * 搜索页面中的歌手页面
 */
public class SingerFragment extends Fragment implements AdapterView.OnItemClickListener {
    private String url;
    private ListView song_listview;
    private VolleySingleton volleySingleton;
    private List<Singer.DataEntity> data;
    private SingerListViewAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.songfragment, null);
        song_listview = (ListView) view.findViewById(R.id.song_listview);
        song_listview.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
        return view;
    }

    private void initData() {
        //得到Seekcontent中传入缓存的值拼接解析
        String q = MyConfig.getSharePreStr(getActivity(), "Seekcontent", "url");
        Log.i("dsadsaddasdasdsadsadsa", "++++++++++++++" + q);
        url = "http://so.ard.iyyin.com/s/artist?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&" +
                "size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1&q=" +
                q +
                "&imsi=460078098497828&tid=0";
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                Gson gson = new Gson();
                Singer result = gson.fromJson(str, Singer.class);
                data = result.getData();
                adapter = new SingerListViewAdapter(getActivity(), data);
                song_listview.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Singer.DataEntity entity = (Singer.DataEntity) parent.getItemAtPosition(position);
//把此页面解析的两个值传入缓存中
        if (entity != null) {
            Intent intent = new Intent(getActivity(), SeekSingerActivity.class);
            Log.i("lano+++++", data + "7777777");
            String image = entity.getPic_url();

            int _id = entity.get_id();
            MyConfig.putSharePre(getActivity(),"entiey","_id",_id);

            if (image != null) {
                MyConfig.putSharePre(getActivity(), "entity", "url", image );
                Log.i("********************-------", image);
                getActivity().startActivity(intent);
                getActivity().finish();

            }
        }
    }
}